package com.linyu.huawei.simple;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 选修课
 */
public class ElectiveCourses {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int courseCount = 2;
        System.out.println("请依据规则输入（每行对应选修课所有成绩）：");
        String[] allStrArr = new String[courseCount];
        for (int i = 0; i < courseCount; i++) {
            allStrArr[i] = sc.next();
        }

        // 输入完毕，进行处理逻辑
        HashMap<String, String> firstCourse = new HashMap<>(); // 记录学生选修记录
        HashMap<String, String> secondCourse = new HashMap<>(); // 记录学生选修记录

        String[] infosFirstArr = allStrArr[0].split(";");
        String[] infosSecondArr = allStrArr[1].split(";");

        for (String str : infosFirstArr) {
            String[] infoArr = str.split(",");
            firstCourse.put(infoArr[0], infoArr[1]);
        }

        for (String str : infosSecondArr) {
            String[] infoArr = str.split(",");
            secondCourse.put(infoArr[0], infoArr[1]);
        }

        HashMap<String,Integer> allStudyList = new HashMap<>(); // 准备容器存储学号
        for (Map.Entry<String, String> stringStringEntry : firstCourse.entrySet()) {
            if (secondCourse.containsKey(stringStringEntry.getKey())){
                allStudyList.put(stringStringEntry.getKey(), Integer.parseInt(stringStringEntry.getValue()) + Integer.parseInt(secondCourse.get(stringStringEntry.getKey()))); // 存储学号
            }
        }
        if (allStudyList.isEmpty()){
            System.out.println("NULL"); // 没有就直接返回
            return;
        }
        Set<Integer> classSet = new HashSet<>(); // 准备班级编号容器
        for (Map.Entry<String, Integer> entry : allStudyList.entrySet()) {
            String classNo = entry.getKey().substring(0, 5);
            classSet.add(Integer.parseInt(classNo));
        }
        Map<Integer, List<Map.Entry<String, Integer>>> resultMap = new HashMap();
        classSet.forEach(one ->{
            ArrayList<Map.Entry<String, Integer>> nowList = new ArrayList<>();
            // 去找对应的学生
            for (Map.Entry<String, Integer> stuMap : allStudyList.entrySet()) {
                if (Integer.parseInt(stuMap.getKey().substring(0, 5)) == one){
                    nowList.add(stuMap);
                }
            }
            List<Map.Entry<String, Integer>> collect = nowList.stream().sorted((o1, o2) -> {
                if (!o1.getValue().equals(o2.getValue())) {
                    return o1.getValue() - o2.getValue();
                }
                return Integer.parseInt(o1.getKey()) - Integer.parseInt(o2.getKey());
            }).collect(Collectors.toList());
            resultMap.put(one, collect);
        });
        System.out.println("====== 开始打印结果 ======");
        resultMap.forEach((k, v) ->{
            System.out.println(k);
            for (int i = 0; i < v.size(); i++) {
                System.out.print(v.get(i).getKey());
                if (i != v.size() - 1){
                    System.out.print(";");
                }
            }
            System.out.println();
        });

    }

}
